package com.base.self.controller;

import com.base.log.LoginAn;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.self.dto.commodity.*;
import com.base.self.entity.Commodity;
import com.base.self.service.ICommodityService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author XiFYuW
 * @since 2020-11-15
 */
@RestController
@RequestMapping("/admin/commodity")
public class CommodityController {

    private final ICommodityService iCommodityService;

    public CommodityController(ICommodityService iCommodityService) {
        this.iCommodityService = iCommodityService;
    }

    @GetMapping(value = "/commodityIndex")
    @LoginAn
    public ModelAndView commodityIndex() {
        return new ModelAndView("self/index_commodity");
    }

    @PostMapping(value = "/commodityWebList")
    @Validated
    @LoginAn(isRe = true)
    public ResponseResult commodityWebList(@Valid final CommodityWebListDTO commodityWebListDTO) {
        return iCommodityService.commodityWebList(commodityWebListDTO);
    }

    @PostMapping(value = "/commodityWebAdd")
    @Validated
    @LoginAn(isRe = true)
    public ResponseResult commodityWebAdd(@Valid final CommodityWebAddDTO commodityWebAddDTO) {
        return iCommodityService.commodityWebAdd(commodityWebAddDTO);
    }

    @PostMapping(value = "/commodityWebDelete")
    @Validated
    @LoginAn(isRe = true)
    public ResponseResult commodityWebDelete(@Valid final CommodityWebDeleteDTO commodityWebDeleteDTO) {
        return iCommodityService.commodityWebDelete(commodityWebDeleteDTO);
    }

    @PostMapping(value = "/commodityWebUpdate")
    @Validated
    @LoginAn(isRe = true)
    public ResponseResult commodityWebUpdate(@Valid final CommodityWebUpdateDTO commodityWebUpdateDTO) {
        return iCommodityService.commodityWebUpdate(commodityWebUpdateDTO);
    }

    @PostMapping(value = "/commodityWebDetail")
    @Validated
    @LoginAn(isRe = true)
    public ResponseResult commodityWebDetail(@Valid final CommodityWebDetailDTO commodityWebDetailDTO) {
        return iCommodityService.commodityWebDetail(commodityWebDetailDTO);
    }

    @PostMapping("/canBeBuy")
    @ResponseBody
    @ApiOperation("是否可被购买")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "商品id"),
            @ApiImplicitParam(name = "buy",value = "是否可被购买 0否 1是")
    })
    public ResponseResult canBeBuy(Integer id , Integer buy){
        Commodity commodity = new Commodity();
        commodity.setId(id);
        commodity.setBuy(buy);
        iCommodityService.updateById(commodity);
        return ResponseResultUtils.getResponseResultS("设置成功");
    }
}

